package com.muyu.payment.controller;

import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.payment.domain.PaymentConfigs;
import com.muyu.payment.domain.req.PaymentConfigsQueryReq;
import com.muyu.payment.domain.model.PaymentConfigsQueryModel;
import com.muyu.payment.domain.model.PaymentConfigsPageQueryModel;
import com.muyu.payment.domain.resp.PaymentConfigsPageResp;
import com.muyu.payment.domain.req.PaymentConfigsSaveReq;
import com.muyu.payment.domain.req.PaymentConfigsEditReq;
import com.muyu.payment.domain.model.PaymentConfigsSaveModel;
import com.muyu.payment.domain.model.PaymentConfigsEditModel;
import com.muyu.payment.service.PaymentConfigsService;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;

/**
 * 支付配置Controller
 *
 * @author muyu
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/configs")
@Tag(name = "支付配置", description = "支付配置控制层")
public class PaymentConfigsController extends BaseController {

    /**
    * 支付配置业务层
    */
    @Autowired
    private PaymentConfigsService paymentConfigsService;

    /**
     * 查询支付配置列表
     */
    @Operation(summary = "获取支付配置列表", description = "获取支付配置列表")
    @RequiresPermissions("payment:configs:list")
    @GetMapping("/list")
    public Result<DataPageResp<PaymentConfigsPageResp>> list(PaymentConfigsQueryReq paymentConfigsQueryReq) {
        PageQueryModel<PaymentConfigsPageQueryModel> pageQueryModel = paymentConfigsService.pageQuery(PaymentConfigsQueryModel.reqBuild(paymentConfigsQueryReq));
        return Result.success(DataPageResp.of(pageQueryModel).cover(PaymentConfigsPageResp::infoBuild));
    }

    /**
     * 导出支付配置列表
     */
    @Operation(summary = "导出支付配置列表", description = "导出支付配置列表")
    @RequiresPermissions("payment:configs:export")
    @Log(title = "支付配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentConfigs paymentConfigs) {
        List<PaymentConfigs> list = paymentConfigsService.exportList(paymentConfigs);
        ExcelUtil<PaymentConfigs> util = new ExcelUtil<>(PaymentConfigs.class);
        util.exportExcel(response, list, "支付配置数据");
    }

    /**
     * 获取支付配置详细信息
     */
    @Operation(summary = "获取支付配置详细信息", description = "获取支付配置详细信息")
    @RequiresPermissions("payment:configs:query")
    @GetMapping(value = "/{paymentId}")
    public Result<PaymentConfigs> getInfo(@PathVariable("paymentId") Long paymentId) {
        return Result.success(paymentConfigsService.getById(paymentId));
    }

    /**
     * 新增支付配置
     */
    @RequiresPermissions("payment:configs:add")
    @Log(title = "支付配置", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增支付配置", description = "新增支付配置")
    public Result<String> add(@RequestBody PaymentConfigsSaveReq paymentConfigsSaveReq) {
        paymentConfigsService.save(PaymentConfigsSaveModel.saveReqBuild(paymentConfigsSaveReq));
        return Result.success();
    }

    /**
     * 修改支付配置
     */
    @RequiresPermissions("payment:configs:edit")
    @Log(title = "支付配置", businessType = BusinessType.UPDATE)
    @PutMapping("/{paymentId}")
    @Operation(summary = "修改支付配置", description = "修改支付配置")
    public Result<String> edit(@PathVariable(value = "paymentId") Long paymentId, @RequestBody PaymentConfigsEditReq paymentConfigsEditReq) {
        paymentConfigsService.edit(PaymentConfigsEditModel.editReqBuild(paymentId,paymentConfigsEditReq));
        return Result.success();
    }

    /**
     * 删除支付配置
     */
    @RequiresPermissions("payment:configs:remove")
    @Log(title = "支付配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paymentIds}")
    @Operation(summary = "删除支付配置", description = "删除支付配置")
    public Result<String> remove(@PathVariable(value = "paymentIds") List<Long> paymentIds) {
        return toAjax(paymentConfigsService.removeBatchByIds(paymentIds));
    }
}
